import { Button } from 'antd';
import React from 'react';
interface Props {}
const PageA: React.FC<Props> = (props) => {
  const {} = props;
  const channel = new BroadcastChannel('text');
  let openPageNum = 0;
  // 监听来自页面B的消息
  channel.onmessage = (event) => {
    if (event.data.data === 'close') {
      openPageNum--;
    }
  };

  const handleClick = (name) => {
    console.log('....', openPageNum);

    if (openPageNum) {
      channel.postMessage(name);
    } else {
      window.open(`/pageB/#${name}`, 'text');
    }
    openPageNum++;
  };
  return (
    <div>
      <Button onClick={() => handleClick('123')}>跳转页面B</Button>
      <Button onClick={() => handleClick('456')}>跳转页面B</Button>
      <Button onClick={() => handleClick('789')}>跳转页面B</Button>
    </div>
  );
};
export default PageA;
